<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="88px">
      <el-form-item label="机构" prop="departmentId">
        <el-select
          v-model="queryParams.departmentId"
          placeholder="请选择机构"
          filterable
          clearable
          @change="deptChange"
        >
          <el-option
            v-for="item in departmentList"
            :key="item.id"
            :label="item.name"
            :value="item.id"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="停车场" prop="parkId">
          <el-select v-model="queryParams.parkId" placeholder="请选择" clearable >
            <el-option
              v-for="item in allParkList"
              :key="item.id"
              :label="item.name"
              :value="item.id"/>
          </el-select>
        </el-form-item>
      <el-form-item label="车牌号码" prop="plateNumber">
        <el-input
          v-model="queryParams.plateNumber"
          placeholder="请输入车牌号码"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="车辆类型" prop="type">
        <el-select v-model="queryParams.type" placeholder="请选择车辆类型" clearable>
          <el-option
            v-for="item in dict.type.ECarcomeType"
            :key="item.value"
            :label="item.label"
            :value="item.value"/>
        </el-select>
      </el-form-item>
      <el-form-item label="泊位编码" prop="berthCode">
        <el-input
          v-model="queryParams.berthCode"
          placeholder="请输入泊位编码"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="昵称" prop="nickname">
        <el-input
          v-model="queryParams.nickname"
          placeholder="请输入昵称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="手机号码" prop="phoneNumber">
        <el-input
          v-model="queryParams.phoneNumber"
          placeholder="请输入手机号码"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="订单号" prop="orderId">
        <el-input
          v-model="queryParams.orderId"
          placeholder="请输入订单号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="驶入时间" prop="dateRange">
        <el-date-picker
          v-model="queryParams.dateRange"
          value-format="yyyy-MM-dd HH:mm:ss"
          type="datetimerange"
          range-separator="-"
          start-placeholder="开始时间"
          end-placeholder="结束时间"
          style="width:333px;"
          :default-time="['00:00:00','23:59:59']"
        ></el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">查询</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <div class="biankuan flex_hcneter borderLeft_1">
      <div class="contont">
        <p class="num txt-ellipsis">{{ total }}</p>
        <p class="title">停车次数（次）</p>
      </div>

    </div>

    <el-table v-loading="loading" :data="spparkplaceList" >
      <el-table-column
      v-for="(item,index) in tableList"
      :key="index"
      :label="item.label" align="center" :prop="item.prop" :width="item.width ? item.width : ''">
        <template
        slot-scope="{row}">
          <dict-tag v-if="item.prop == 'registerType'" :options="dict.type.ECarcomeRegisterType" :value="row[item.prop]"/>
          <dict-tag v-else-if="item.prop == 'userLoginChannel'" :options="dict.type.ELoginChannel" :value="row[item.prop]"/>
          <dict-tag v-else-if="item.prop == 'type'" :options="dict.type.ECarcomeType" :value="row[item.prop]"/>
          <dict-tag v-else-if="item.prop == 'status'" :options="dict.type.ECarcomeStatus" :value="row[item.prop]"/>
          <div v-else>{{row[item.prop]}}</div>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" width="300">
        <template slot-scope="{row}">
          <div>
            <el-button
              size="mini"
              icon="el-icon-view"
              v-if="row.id"
              @click="handleUpdate(row)"
              type="text"
            >详情
            </el-button>
            <el-button
              size="mini"
              icon="el-icon-document-delete"
              v-if="row.status != 3"
              v-hasPermi="['admin:carcome:operationLogList1']"
              @click="closeOrder(row)"
              type="text"
            >结束订单
            </el-button>
            <el-button
              size="mini"
              icon="el-icon-document"
              v-if="row.id"
              v-hasPermi="['admin:carcome:operationLogList1']"
              @click="handleCaozuo(row)"
              type="text"
            >操作日志
            </el-button>
            
          </div>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.page"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 停车详情弹框 -->
    <el-dialog :close-on-click-modal="false" :title="title" :visible.sync="open" width="1000px" append-to-body>
      <div v-if="title == '操作日志'">
        <el-table v-loading="rizhiLodding" :data="rizhiList" >
          <el-table-column
          v-for="(item,index) in rzTableList"
          :key="index"
          :label="item.label" align="center" :prop="item.prop" :width="item.width ? item.width : ''">
            <template
            slot-scope="{row}">
              <dict-tag v-if="item.prop == 'type'" :options="dict.type.ECarcomeOperationLogType" :value="row[item.prop]"/>
              <div v-else-if="item.prop == 'img'">
                <el-image 
                  v-if="row[item.prop]"
                  style="width: 100px; height: 100px"
                  :src="row[item.prop]" 
                  :preview-src-list="[row[item.prop]]">
                </el-image>
              </div>
              <div v-else>{{row[item.prop]}}</div>
            </template>
          </el-table-column>
        </el-table>

        <pagination
          v-show="rizhiTotal>0"
          :total="rizhiTotal"
          :page.sync="rizhiQueryParams.page"
          :limit.sync="rizhiQueryParams.pageSize"
          @pagination="operationLogList"
        />
      </div>
      <div class="diy-mx-10" v-else>
        <el-form
          ref="forms_details"
          :model="forms_detail"
          label-width="110px"
        >
          <el-row>
            <el-col :span="12">
              <el-form-item label="片区名称">
                <div class="diy-block-box diy-h-10 diy-leading-10 diy-text-sm">
                  {{ forms_detail.parkName || '' }}
                </div>
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="泊位号">
                <div class="diy-block-box diy-h-10 diy-leading-10 diy-text-sm">
                  {{ forms_detail.berthCode || '' }}
                </div>
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="车牌号码">
                <div class="diy-block-box diy-h-10 diy-leading-10 diy-text-sm">
                  {{ forms_detail.plateNumber || '' }}
                </div>
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="车辆类型">
                <div class="diy-block-box diy-h-10 flex_hcneter diy-text-sm" style="padding-left: 12px;">
                  <dict-tag style="text-indent: 0px;" :options="dict.type.ECarcomeType" :value="forms_detail.type"/>
                </div>
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="登记时间">
                <div class="diy-block-box diy-h-10 diy-leading-10 diy-text-sm">
                  {{ forms_detail.registerTime }}
                </div>
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="登记方式">
                <div class="diy-block-box diy-h-10 diy-leading-10 diy-text-sm">
                  <dict-tag :options="dict.type.ECarcomeRegisterType" :value="forms_detail.registerType"/>
                </div>
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="驶入时间">
                <div class="diy-block-box diy-h-10 diy-leading-10 diy-text-sm">
                  {{ forms_detail.enterTime }}
                </div>
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="驶出时间">
                <div class="diy-block-box diy-h-10 diy-leading-10 diy-text-sm">
                  {{ forms_detail.outTime }}
                </div>
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="停车时长">
                <div class="diy-block-box diy-h-10 diy-leading-10 diy-text-sm">
                  {{ forms_detail.durationStr }}
                </div>
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="应收金额">
                <div class="diy-block-box diy-h-10 diy-leading-10 diy-text-sm">
                  {{ forms_detail.totalAmount }}
                </div>
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="优惠金额">
                <div class="diy-block-box diy-h-10 diy-leading-10 diy-text-sm">
                  {{ forms_detail.discountAmount }}
                </div>
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="实收金额">
                <div class="diy-block-box diy-h-10 diy-leading-10 diy-text-sm">
                  {{ forms_detail.paidAmount }}
                </div>
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="欠费金额">
                <div class="diy-block-box diy-h-10 diy-leading-10 diy-text-sm">
                  {{ forms_detail.actualAmount }}
                </div>
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="状态">
                <div class="diy-block-box diy-h-10 diy-leading-10 diy-text-sm" style="padding-left: 12px;">
                  <dict-tag style="text-indent: 0px;" :options="dict.type.ECarcomeStatus" :value="forms_detail.status"/>
                </div>
              </el-form-item>
            </el-col>
          </el-row>
        </el-form>
      </div>
      <div slot="footer" class="dialog-footer">
        <el-button @click="open = false">取 消</el-button>
      </div>
    </el-dialog>

    <!-- 修改车牌号弹框 -->
    <el-dialog :title="titlePlate" :visible.sync="openPlate" width="600px" append-to-body>
      <el-form ref="form_plate" :model="form_plate" :rules="rules_plate" label-width="120px">
        <el-row>
          <el-col :span="24">
            <el-form-item label="结束时间" prop="endTime">
              <el-date-picker
                v-model="form_plate.endTime"
                value-format="yyyy-MM-dd HH:mm:ss"
                type="datetime"
                
              ></el-date-picker>
            </el-form-item>
          </el-col>
        </el-row>

      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" :loading="pateLoding" @click="submitPlate">确 定</el-button>
        <el-button @click="openPlate = false">取 消</el-button>
      </div>
    </el-dialog>

  </div>
</template>

<script>
import {fetchList,getOutdoor,operationLogList,completeOrder} from "@/api/park/parkRecords";
import { getDepartmentList,getListAuthPark } from '@/api/base'
import { YyMmDdToTimes } from "@/utils/time.js";
export default {
  name: "parkRecords",
  dicts: ['ECarcomeStatus','ECarcomeRegisterType','ELoginChannel','ECarcomeType','ECarcomeOperationLogType'],
  components:{},
  data() {
    return {
      pateLoding:false,//加载状态
      // 弹出层标题
      title: "",
      titlePlate: "",
      // 是否显示弹出层
      open: false,
      openPlate: false,
      form_plate: {},
      rules_plate: {
        endTime:[
          {required: true, message: "结束时间不能为空", trigger: "blur"}
        ],
      },
      // 表单参数
      forms_detail: {},
      rzTableList:[
        {label:'操作日志类型',prop:'type',},
        {label:'旧值',prop:'oldValue',},
        {label:'新值',prop:'newValue',},
        {label:'图片地址',prop:'img',},
        {label:'操作人',prop:'createBy',},
        {label:'操作时间',prop:'createTime',},
      ],//日志表格字段显示
      tableList:[
        {label:'机构名称',prop:'departmentName',},
        {label:'片区名称',prop:'parkName',},
        {label:'泊位编码',prop:'berthCode',},
        {label:'订单号',prop:'orderId',},
        {label:'车牌号码',prop:'plateNumber',},
        {label:'车辆类型',prop:'type',},
        {label:'昵称',prop:'nickname',},
        {label:'电话号码',prop:'phoneNumber',},
        {label:'用户登录渠道',prop:'userLoginChannel',},
        {label:'停车时长',prop:'durationStr',},
        {label:'入场时间',prop:'enterTime',},
        {label:'出场时间',prop:'outTime',},
        {label:'登记方式',prop:'registerType',},
        {label:'登记时间',prop:'registerTime',},
        {label:'状态',prop:'status',},
      ],//表格字段显示
      //片区数据
      areaList: [],
      // 遮罩层
      loading: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 车位信息表格数据
      spparkplaceList: [],
      // 查询参数
      queryParams: {
        page: 1,
        pageSize: 10,
        dateRange:[],
      },
      departmentList: [],//机构数据
      allParkList: [],//停车场数据
      //日志相关
      rizhiQueryParams: {
        page: 1,
        pageSize: 10,
      },
      rizhiTotal:0,
      rizhiList:[],
      rizhiLodding:false,
    };
  },
  created() {
    this.getDepartmentList();
    this.getList();
  },
  methods: {
    //保存结束时间
    submitPlate() {
      this.$refs["form_plate"].validate(valid => {
        if (valid) {
          this.pateLoding = true
          if(this.titlePlate == '结束订单'){
            let nowTime = new Date().getTime()
            let time = YyMmDdToTimes(this.form_plate.endTime)
            if(time > nowTime) {
              this.pateLoding = false
              this.$modal.msgWarning("选择的时间不能大于当前时间");
              return
            }
            completeOrder(this.form_plate).then(response => {
              this.pateLoding = false
              this.$modal.msgSuccess("操作成功");
              this.openPlate = false;
              
              this.getList();
            }).catch(()=>{this.pateLoding = false});
          }
        }
      });
    },
    closeOrder(row){//结束订单 
      this.form_plate = {id:row.id}
      this.openPlate = true
      this.titlePlate = '结束订单';
    },
    handleCaozuo(row){//操作日志按钮
      this.rizhiQueryParams.id = row.id
      this.operationLogList()
      this.title = '操作日志'
      this.open = true
    },
    async operationLogList(){//操作日志数据
      this.rizhiLodding = true
      let res = await operationLogList(this.rizhiQueryParams)
      let data = res.data || {}
      let list = data.list || []
      this.rizhiTotal = data.total || 0
      this.rizhiList = list
      this.rizhiLodding = false
    },
    /** 详情按钮操作 */
    handleUpdate(row) {
      const pkParkrecordId = row.id;
      getOutdoor(pkParkrecordId).then(response => {
        let data = response.data || {}
        this.forms_detail = data;
        this.open = true;
        this.title = "停车记录详情";
      });
    },
    async deptChange(e){//机构下拉选择
      let queryParams = {...this.queryParams}
      queryParams.parkId = ''
      this.queryParams = {...queryParams}
      if(!e) return this.allParkList = []
      this.allParkList = await this.getListAuthPark(e)
    },
    getDepartmentList(){//获取机构
      getDepartmentList().then(res => {
        this.departmentList = res.data || []
      })
    },
    async getListAuthPark(departmentId){//获取停车场
      let res = await getListAuthPark({departmentId,parkType:2})
      return res.data || []
    },
    /** 查询车位信息列表 */
    getList() {
      this.loading = true;
      let queryParams = {
        ...this.queryParams
      }
      if(!queryParams.dateRange) queryParams.dateRange = []
      if(queryParams.dateRange.length) {
        queryParams.startTime = queryParams.dateRange[0]
        queryParams.endTime = queryParams.dateRange[1]
      }
      delete queryParams.dateRange
      delete queryParams.date
      fetchList(queryParams).then(response => {
        let data = response.data || {}
        let rows = data.list || []
        this.spparkplaceList = rows
        this.total = data.total || 0;
        this.loading = false;
      });
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.page = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.queryParams.dateRange = []
      this.handleQuery();
    },
  }
};
</script>
<style lang="scss">
 .amap-icon img,.amap-icon,
.amap-marker-content img{
    width: 25px!important;
    height: 34px!important;
}
#container {width:650px; height: 300px; }
</style>
